package com.hui.service.impl;

import com.hui.bean.RoomBean;
import com.hui.dao.impl.RoomDaoImpl;
import com.hui.service.IRoomService;

import java.sql.SQLException;
import java.util.List;

/**
 * 房间相关业务层实现类
 *
 */
public class RoomServiceImpl implements IRoomService {

    private RoomDaoImpl roomDao;

    public RoomServiceImpl(){
        try {
            roomDao=RoomDaoImpl.class.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<RoomBean> getAllRooms() {
        try {
            return roomDao.getAllRooms();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public List<RoomBean> findRoomsByCondition(String roomAddress, String roomStatus, String availableLive, String roomType) {
        try {
            return roomDao.findRoomsByCondition(roomAddress,roomStatus,availableLive,roomType);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean addRoom(RoomBean roomBean) {
        try {
            return roomDao.saveRoom(roomBean)==1;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean removeRoomByRoomId(int roomId) {
        try {
            return roomDao.deleteRoomByRoomId(roomId)==1;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public RoomBean showRoomInfo(int roomId) {
        try {
            return roomDao.getRoomByRoomId(roomId);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public List<RoomBean> getCanInRoomsByRoomType(String roomType) {

        try {
            return roomDao.getAvailableRooms(roomType);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public RoomBean getRoomByRoomAddress(String roomAddress) {
        if (roomAddress!=null){
            try {
               return roomDao.getRoomByRoomAddress(roomAddress);
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public boolean isExitRoomByRoomAddress(String roomAddress) {
        try {
            RoomBean roomBean = roomDao.getRoomByRoomAddress(roomAddress);
            if (null==roomBean.getRoomAddress()){
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return true;
        }
        return true;
    }

    @Override
    public int removeStudentFromRoomByRoomId(int roomId) {
        try {
            roomDao.removeStudentFromRoomByRoomId(roomId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateRoomStatusToEorror(int roomId) {
        return 0;
    }

    @Override
    public int updateRoomStatusToNormal(int roomId) {
        return 0;
    }


}
